import { createStore } from 'vuex'
import components from './modules/components'
import wbComponents from './modules/wb-components'
import cardComponents from './modules/card-components'
import user from './modules/user'

export const EventBus = window.mitt();
EventBus.$off = EventBus.off;
EventBus.$on = EventBus.on;
EventBus.$emit = EventBus.emit;

const state = {
  globalIsPost: {},
  formValues: {},
  imgCropPop: {
    isActive:false,
    url:"",
    previews: {},
  },
  editPathAnimate: {
    isActive: false,
  },
  mediaPop: {
    isActive: false,
    type: 0,
    item: {},
    index: -1,
    comAttr: '',//更新组件中的哪个按钮的键值
    picAttr: '',//属性的键值，同一个中有两个属性需要添加图片
    event: null,
  },
  deleteSiteData: {
    isDelete: false,
    site_id: '',
  },
  popData: {
    isActive: false,
    type: 'pop-success',//1成功 2 提交表单 3 失败,
    isForm: false,
    panelAttr: null,
  },
  selectEvent: {
    isActive: false,
    type: '',
  },
  selectPageEvent: {
    isActive: false,
    type: '',
  },
  selectAnimate: {
    isActive: false,
    index: -1,
  },
  isShowLogin: false,
  isShowRegister: false,
  isLeavePageInfo: {
    isLeavePage: false,
    router: null,
    path: ''
  },
  siteInfo: {
    md: {}
  },
}
const mutations = {
  updateDeleteSiteData(state, {isDelete, site_id}){
    state.deleteSiteData.isDelete = isDelete
    state.deleteSiteData.site_id = site_id
  },
  setEditPathAimate(state, params){
    _.merge(state.editPathAnimate, params);
  },
  setImgCropPop(state, params){
    state.mediaPop.picAttr =  '';
    state.mediaPop.comAttr = ''
    _.merge(state.mediaPop, params)
    state.imgCropPop.isActive = true;
    state.imgCropPop.url = params.url;
  },
  setImgCropPreview(state, params){
    state.imgCropPop.previews = params;
  },
  hiddenImgCropPop(){
    state.imgCropPop.isActive = false;
    state.imgCropPop.previews.url = "";
    state.imgCropPop.url = "";
  },
  setMediaPop(state, params){
    state.mediaPop.picAttr =  '';
    state.mediaPop.comAttr = '';
    state.mediaPop.event = null;
    state.mediaPop.item = {};
    _.merge(state.mediaPop, params)
    state.mediaPop.isActive = true;
    EventBus.$emit('refreshMediaPop');
  },
  hiddenMediaPop(){
    state.mediaPop.picAttr =  '';
    state.mediaPop.comAttr = ''
    state.mediaPop.isActive = false;
    EventBus.$emit('refreshMediaPop');
  },
  changeSelectAnimatePlane(state, data){
    state.selectAnimate.isActive = data.isActive
    state.selectAnimate.index = data.index
  },
  changeSelectEventPlane(state, data){
    state.selectEvent.isActive = data.isActive;
    state.selectEvent.type = data.type;
  },
  changeSelectPageEventPlane(state, data){
    state.selectPageEvent.isActive = data.isActive;
    state.selectPageEvent.type = data.type;
  },
  updateRegisterStatus(state, data){
    state.isShowRegister = data;
  },
  updateLoginStatus(state, data){
    state.isShowLogin = data;
  },
  updateIsLeavePage(state, data){
    _.merge(state.isLeavePageInfo, data)
  },
  updateComponentVisible() {
  }
  // recordMousePosition (state, position) {
  //   state.position.left = position.left;
  //   state.position.top = position.top;
  // }
  // increment (state) {
  //   state.count++
  // },
}
const actions = {
  setEditPathAimate: ({commit}, params) => {
    // commit('hiddenMediaPop', params);
    commit('setEditPathAimate', params);
  },
  setImgCropPop: ({commit}, params) => {
    commit('hiddenMediaPop', params);
    commit('setImgCropPop', params);
  },
  hiddenImgCropPop: ({commit}) => commit('hiddenImgCropPop'),
  setMediaPop: (store, params) => {
    let {commit} = store;
    commit('hiddenImgCropPop', params);
    commit('setMediaPop', params);
  },
  setImgCropPreview: ({commit}, params) => commit('setImgCropPreview', params),
  hiddenMediaPop: ({commit}) => commit('hiddenMediaPop'),
  changeSelectAnimatePlane: ({commit}, params) => commit('changeSelectAnimatePlane', params),
  changeSelectEventPlane: ({commit}, params) => commit('changeSelectEventPlane', params),
  changeSelectPageEventPlane: ({commit}, params) => commit('changeSelectPageEventPlane', params),
  updateRegisterStatus: ({commit}, params) => commit('updateRegisterStatus', params),
  updateLoginStatus: ({commit}, params) => commit('updateLoginStatus', params),

  // recordMousePosition: ({commit}, position) => commit('recordMousePosition', position),
  // increment: ({ commit }) => commit('increment'),
}
const getters = {
  isSwiper: () => {
    return false
  }
  // evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
}

const store = createStore({
  state,
  mutations,
  actions,
  getters,
  modules: {
    components,
    wbComponents,
    user,
    cardComponents
  },
  // strict: debug,
})

export default store
